package com.zhike.api.controller.friend;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhike.common.dto.CMDBodyAddFriendRequest;
import com.zhike.common.dto.FriendDeleteDTO;
import com.zhike.common.dto.FriendOfflineDTO;
import com.zhike.common.dto.FriendRemarkDTO;
import com.zhike.common.dto.FriendRequestDTO;
import com.zhike.common.dto.FriendUIdDTO;
import com.zhike.common.dto.R;
import com.zhike.common.dto.UserRosterVO;
import com.zhike.common.dto.UserUIdDTO;
import com.zhike.common.enums.ResultCode;
import com.zhike.common.enums.YesNoEnum;
import com.zhike.common.exception.ZkimException;
import com.zhike.common.vo.OfflineFriendsRequestVO;
import com.zhike.common.vo.UserInfo4Cache;
import com.zhike.service.friends.IFriendsService;
import com.zhike.service.messages.IChatSessionService;
import com.zhike.service.system.ISystemService;
import com.zhike.service.user.IUserBlacklistService;
import com.zhike.utils.cache.UsersInfoCacheProvider;
import com.zhike.utils.file.FileAvatar;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/zkim/friend")
@Api(tags = "好友模块")
public class FriendController {

  @Resource
  private IFriendsService friendsService;

  @Autowired
  private ISystemService systemService;

  @Autowired
  private FileAvatar fileAvatar;

  @Autowired
  private IChatSessionService chatSessionService;

  @Autowired
  private IUserBlacklistService userBlacklistService;

  @Autowired
  private UsersInfoCacheProvider usersInfoCacheProvider;

  @ApiOperation(value = "好友列表", notes = "获取用户好友列表接口")
  @PostMapping("queryFriendsList")
  public R<List<UserRosterVO>> queryFriendsList() {
    UserUIdDTO dto = new UserUIdDTO();
    UserInfo4Cache uc = systemService.getUserInfo();
    dto.setUid(uc.getFakeUid());
    return R.success(friendsService.queryFriendsList(dto));
  }

  @ApiOperation(value = "添加好友请求", notes = "发起添加好友请求的接口; 参数srcFrom：0其它1群聊2二维码3名片")
  @PostMapping("sendAddFriendRequest")
  public R<Boolean> sendAddFriendRequest(@Validated @RequestBody FriendRequestDTO dto) throws Exception {

    UserInfo4Cache uc = systemService.getUserInfo();
    // 校验是否在我的黑名单中
    if (userBlacklistService.checkInBlacklist(uc.getFakeUid(), dto.getFriendFakeUId())) {
      return R.rsp(ResultCode.FRIEND_IN_BLACKLIST);
    }

    UserInfo4Cache friend = usersInfoCacheProvider.get(dto.getFriendFakeUId());
    switch (dto.getSrcFrom()) {
      case "1":  //群
        if (friend.getAddMethodGroup().equals(YesNoEnum.NO.getValue().toString())) {
          throw new ZkimException(ResultCode.FRIEND_NOT_ALLOW);
        }
        break;
      case "2":  //二维码
        if (friend.getAddMethodQr().equals(YesNoEnum.NO.getValue().toString())) {
          throw new ZkimException(ResultCode.FRIEND_NOT_ALLOW);
        }
        break;
      case "3":  //名片
        if (friend.getAddMethodCard().equals(YesNoEnum.NO.getValue().toString())) {
          throw new ZkimException(ResultCode.FRIEND_NOT_ALLOW);
        }
        break;
      default:

    }

    FriendUIdDTO request = new FriendUIdDTO();
    request.setFriendUid(dto.getFriendFakeUId());
    request.setDesc(dto.getDesc());
    return R.success(friendsService.sendAddFriendRequest(uc.getFakeUid(), request));
  }

  @ApiOperation(value = "同意加好友请求", notes = "被添加者【同意】加好友请求的接口")
  @PostMapping("agreeFriend")
  public R<Boolean> agreeFriend(@Validated @RequestBody FriendUIdDTO dto) throws Exception {
    return R.success(friendsService.agreeFriend(dto));
  }

  @ApiOperation(value = "拒绝加好友请求", notes = "被添加者【拒绝】加好友请求的接口")
  @PostMapping("refuseFriend")
  public R<Boolean> refuseFriend(@Validated @RequestBody FriendUIdDTO dto) throws Exception {
    return R.success(friendsService.refuseFriend(dto));
  }

  @ApiOperation(value = "删除指定好友接口", notes = "删除指定好友接口")
  @PostMapping("deleteFriend")
  public R deleteFriend(@Valid @RequestBody FriendDeleteDTO dto) {
    return friendsService.deleteFriend(dto);
  }

  @ApiOperation(value = "更新好友信息中的备注、描述等的接口", notes = "更新好友信息中的备注、描述等的接口")
  @PostMapping("updateFriendRemark")
  public R updateFriendRemark(@RequestBody FriendRemarkDTO dto) throws Exception {
    UserInfo4Cache uc = systemService.getUserInfo();
    return R.success(friendsService.updateFriendRemark(uc.getFakeUid(), dto));
  }

  // @ApiOperation(value = "获取离线聊天消息的接口", notes = "获取离线聊天消息的接口")
  // @PostMapping("getOfflineFriendsMessages")
  public R getOfflineFriendsMessages(@RequestBody FriendOfflineDTO dto) {
    return friendsService.getOfflineFriendsMessages(dto);
  }

  @ApiOperation(value = "未处理的加好友请求总数", notes = "获取未处理的加好友请求总数的接口")
  @PostMapping("queryFriendRequestCount")
  public R queryFriendRequestCount(@RequestBody UserUIdDTO dto) throws Exception {
    return friendsService.queryFriendRequestCount(dto);
  }

  @ApiOperation(value = "未处理的加好友请求列表", notes = "查询当前用户的未处理的好友请求")
  @GetMapping("queryFriendRequestList")
  public R<List<OfflineFriendsRequestVO>> queryFriendRequestList() {
    UserInfo4Cache rdsUser = systemService.getUserInfo();
    List<OfflineFriendsRequestVO> list = friendsService.queryFriendRequestList(rdsUser.getFakeUid());
    for (OfflineFriendsRequestVO vo : list) {
      if (rdsUser.getFakeUid().equals(vo.getUserUid())) {
        vo.setAvatar(fileAvatar.getUserAvatarUrl(vo.getBeUserUid()));
        UserInfo4Cache uc = usersInfoCacheProvider.get(vo.getBeUserUid());
        vo.setNickname(uc.getNickName());
      } else if (rdsUser.getFakeUid().equals(vo.getBeUserUid())) {
        vo.setAvatar(fileAvatar.getUserAvatarUrl(vo.getUserUid()));
        UserInfo4Cache uc = usersInfoCacheProvider.get(vo.getUserUid());
        vo.setNickname(uc.getNickName());
      }

    }
    return R.success(list);
  }

  @ApiOperation(value = "标记星")
  @PostMapping("markStar")
  public R<Boolean> markStar(@RequestBody FriendUIdDTO dto) {
    UserInfo4Cache uc = systemService.getUserInfo();
    return R.success(friendsService.markStar(uc.getFakeUid(), dto.getFriendUid()));
  }

  @ApiOperation(value = "取消标记星")
  @PostMapping("unmarkStar")
  public R<Boolean> unmarkStar(@RequestBody FriendUIdDTO dto) {
    UserInfo4Cache uc = systemService.getUserInfo();
    return R.success(friendsService.unmarkStar(uc.getFakeUid(), dto.getFriendUid()));
  }

  // @ApiOperation(value = "获取和好友的会话")
  // @PostMapping("getSessionIdWithFriend")
  public R<String> getSessionIdWithFriend(@RequestBody FriendUIdDTO dto) {
    UserInfo4Cache uc = systemService.getUserInfo();
    return R.success(chatSessionService.getSessionIdWithFriend(uc.getFakeUid(), dto.getFriendUid()));
  }
}
